////////////////////////////////////
//// ASKIMA - COLLABORATOR PROFILE
//// NOTIFICATIONS MANAGEMENT FUNCTIONS
////////////////////////////////////

/**
 * Retrieves user notification and display them
 */

function retrieveNotifications() {

	$.getJSON("findNotifications", function(data) {
		if (data == null) {
			return;
		}
		// For each received notification
		$.each(data, function(i, val) {

			// If the notification is already displayed: continue !
			if (notifications[val.id] != null) {
				// Equivalent of continue in this loop
				return true;
			}

			// To check if the notification is already displayed (for the future
			// call)
			notifications[val.id] = val.message;

			// Add the notification
			$.gritter.add({
				title : 'Notification',
				text : val.message,
				sticky : true,
				after_close : function() {
					// Delete the notification
					$.getJSON("deleteNotification", {
						notificationID : val.id
					}, function() {
						// Refresh the counter
						refreshNotificationCounter(--notificationsCounter);
					});
				}
			});
		});

		// Refresh the counter
		notificationsCounter = data.length;
		refreshNotificationCounter(notificationsCounter);
	});
}

/**
 * Refreshes the notification counter
 * 
 * @param counter
 */

function refreshNotificationCounter(counter) {
	$("#notification_title")
			.text("Vous avez " + counter + " notification(s) !");
}